<?php
namespace App\Http\Controllers;
use App\User;
use App\Article;
use App\Comment;
use Illuminate\Http\Request;
class UserController extends Controller
{
    //
    public function index(){
        $users=User::paginate(15);
        return $users;
    }
    public function edit($id){
        return User::find($id);
    }
    public function update(Request $req){
        $user=User::find($req->id);
        $user->name=$req->name;
        $user->email=$req->email;
        $user->password=bcrypt($req->password);
        //return '更新成功';
        if($user->save()){
            echo '数据更新成功！';
        }
    }
    public function store(Request $req){
        $user=new User;
        $user->name=$req->name;
        $user->email=$req->email;
        $user->password=bcrypt($req->password);
        if($user->save()){
            echo '成功添加新用户';
        }
        
    }
    public function destroy($id){
        if(User::find($id)->delete()){
            echo '删除成功';
        }
        
    }
    public function batchdestroy(Request $req){
        $ids=explode(',',$req->ids);
        foreach ($ids as $id){
            User::find($id)->delete();
        }
        echo '删除成功';
    }
    public function CountAll(){
        $user=User::count();
        $article=Article::count();
        $comment=Comment::count();
        return [
        'user'=>$user,
        'article'=>$article,
        'comment'=>$comment
        ];
        // echo 'hello';
    }
}